package com.example.administrator.tiyuapp.EventFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.administrator.tiyuapp.App;
import com.example.administrator.tiyuapp.Constants;
import com.example.administrator.tiyuapp.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class EventFragment extends Fragment {



    private EventAdapter adapter;
    private List<EventBean.DataBean.ContestListBean> data;

    public EventFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_event, container, false);


            Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolBar);
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        ListView listView = (ListView) view.findViewById(R.id.event_fragment_listView);

        data = new ArrayList<>();
        adapter = new EventAdapter(getContext(),data);
        listView.setAdapter(adapter);

        Spinner spinner = (Spinner) view.findViewById(R.id.SpinnerId);
        ArrayAdapter<String> ada = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.all));
        ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(ada);

        RequestQueue requestQueue = App.getApp().getRequestQueue();



        StringRequest request = new StringRequest(Request.Method.GET, Constants.getHotMatch, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                EventBean eventBean = gson.fromJson(response, EventBean.class);

                for (int i = 0; i < eventBean.getData().getContest_list().size(); i++) {
                    data.add(eventBean.getData().getContest_list().get(i));
                    adapter.notifyDataSetChanged();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        StringRequest request1 = new StringRequest(Request.Method.GET, Constants.getAllMatch, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                EventBean eventBean = gson.fromJson(response, EventBean.class);

                for (int i = 0; i < eventBean.getData().getContest_list().size(); i++) {
                    data.add(eventBean.getData().getContest_list().get(i));
                    adapter.notifyDataSetChanged();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


        request.setTag("hot");
        request1.setTag("all");
        requestQueue.add(request);
        requestQueue.add(request1);


        return view;
    }

}
